package timing.ukulele.form.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import timing.ukulele.form.persistent.EntityConfigDraft;
import timing.ukulele.form.service.EntityConfigDraftService;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.form.service.EntityConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/entityConfigDraft")
@Api(tags = "实体模板配置草稿")
@Slf4j
public class EntityConfigDraftController {
    final EntityConfigDraftService service;
    final EntityConfigService ConfigService;

    public EntityConfigDraftController(EntityConfigDraftService service,
                                       EntityConfigService configService) {
        this.service = service;
        this.ConfigService = configService;
    }

    /**
     * 新增，保存草稿配置接口
     *
     * @param draft
     * @return
     */
    @PostMapping("/save")
    public ResponseResult<EntityConfigDraft> saveConfigDraft(
            @RequestBody EntityConfigDraft draft) {

        return service.saveData(draft);
    }

    /**
     * 草稿配置复制
     *
     * @param existConfigDraftId
     * @return
     */
    @PostMapping("/copy")
    public ResponseResult<Boolean> copyConfigDraft(
            @RequestParam Long existConfigDraftId) {
        return service.copyConfigDraft(existConfigDraftId);
    }

    /**
     * 查询所有的草稿
     *
     * @return
     */
    @GetMapping("/findAll")
    public ResponseResult<List<EntityConfigDraft>> findAll() {
        return ResponseResult.success(service.list());
    }

    @GetMapping("/page")
    @ApiOperation("分页获取列表")
    public ResponseResult<IPage<EntityConfigDraft>> page(
            @ApiParam(value = "系统id", example = "1") @RequestParam Long systemId,
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
            @ApiParam(value = "系统权限ID") @RequestHeader(Constant.SYSTEM) String authSystem) {
        List<String> systemIds = null;
        if (StringUtils.hasLength(authSystem)) {
            String[] split = authSystem.split(",");
            systemIds = Arrays.asList(split);
        }
        if (CollectionUtils.isEmpty(systemIds) || !systemIds.contains(systemId + "")) {
            return ResponseResult.error("您没有操作此系统的权限");
        }
        return ResponseResult.success(service.getPage(systemId, pageNum, pageSize));
    }

    /**
     * 草稿从正式版本
     */
    @PostMapping("/enable")
    public ResponseResult<String> enable(@ApiParam(value = "草稿id", example = "1") @RequestParam(value = "configDraftId") Long configDraftId) {
        return service.enable(configDraftId);
    }
}
